package SoftServeTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ZefiR
 * Date: 16.01.13
 * Time: 23:47
 * To change this template use File | Settings | File Templates.
 */
public class Test
{

    public static void main(String[] args) throws IOException
    {
        //Employee employee1 = new perHourEmployee("John", 1, 5.0);
        SortedSet<Employee> set = new TreeSet<Employee>();
        set.add(new perHourEmployee("John", 1, 5.0));
        set.add(new perMonthEmployee("Jack", 2, 2000));
        set.add(new perHourEmployee("Jina", 3, 10.0));
        try
        {
            PrintWriter out = new PrintWriter("employee.dat");
            writeTreeSet(set, out);
            out.close();
            Scanner in = new Scanner(new FileInputStream("employee.dat"));
            TreeSet<Employee> newSet = readTreeSet(in);
            in.close();
            for (Employee e : newSet)
                System.out.println(e);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }

    public static void writeTreeSet(SortedSet<Employee> set, PrintWriter out) throws IOException
    {
        out.println(set.size());
        for (Employee e : set)
            e.writeData(out);
    }

    public static TreeSet<Employee> readTreeSet(Scanner in)
    {
        int number = in.nextInt();
        in.nextLine();
        System.out.println("В файле найдено " + number + " записи.");
        TreeSet<Employee> temp = new TreeSet<Employee>();
        for (int i = 0; i < number; i++)
        {
            String tmp = in.next();
            //in.nextLine();
            //System.out.println(tmp);
            if (tmp.equals("perHourEmployee"))
            {
                perHourEmployee e = new perHourEmployee();
                e.readData(in);
                temp.add(e);
            } else if (tmp.equals("perMonthEmployee"))
            {
                perMonthEmployee e = new perMonthEmployee();
                e.readData(in);
                temp.add(e);
            }
        }
        return temp;
    }

    static abstract class Employee implements Comparable<Employee>
    {

        String name;
        int idEmployee;
        double payment;

        public abstract double averagePayment(double payment);

        public int compareTo(Employee o)
        {
            if (this.averagePayment(this.payment) < o.averagePayment(o.payment))
                return -1;
            else if (this.averagePayment(this.payment) > o.averagePayment(o.payment))
                return +1;
            else
                return this.name.compareTo(o.name);
        }

        public String toString()
        {
            return idEmployee + " | " + name + " | " + averagePayment(payment);
        }

        public abstract void writeData(PrintWriter out);

        public void readData(Scanner in)
        {
            String str = in.next();
            String[] tmp = str.split("\\|");
            this.idEmployee = Integer.parseInt(tmp[0]);
            this.name = tmp[1];
            this.payment = Double.parseDouble(tmp[2]);
        }
    }

    static class perHourEmployee extends Employee
    {

        public perHourEmployee()
        {

        }

        public perHourEmployee(String name, int id, double payment)
        {
            this.name = name;
            this.idEmployee = id;
            this.payment = payment;
        }

        @Override
        public double averagePayment(double payment)
        {
            return 20.8 * 8 * payment;
        }

        @Override
        public String toString()
        {
            return super.toString();
        }

        public void writeData(PrintWriter out)
        {
            out.println("perHourEmployee");
            out.println(idEmployee + "|" + name + "|" + payment);
        }
    }

    static class perMonthEmployee extends Employee
    {

        public perMonthEmployee(String name, int id, double payment)
        {
            this.name = name;
            this.idEmployee = id;
            this.payment = payment;
        }

        public perMonthEmployee()
        {

        }

        @Override
        public double averagePayment(double payment)
        {
            return payment;
        }

        @Override
        public String toString()
        {
            return super.toString();    //To change body of overridden methods use File | Settings | File Templates.
        }

        public void writeData(PrintWriter out)
        {
            out.println("perMonthEmployee");
            out.println(idEmployee + "|" + name + "|" + payment);
        }


    }

}
